<template>
  <div class="container">
    <div class="top">
      <span>金额：</span>
      <el-input
        style="width: 200px;margin-bottom: 5px;"
        v-model="price"
        :disabled="true"
      ></el-input>
      <el-select
        v-model="search.area"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请输入区域"
        :remote-method="insideRemoteMethods"
        :loading="insideArea.loading"
        @change="area"
      >
        <el-option
          v-for="item in insideArea.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 250px"
        v-model="search.searchCode"
        placeholder="请输入单号"
      ></el-input>
      <el-input
        style="width: 250px"
        v-model="search.pay_code"
        placeholder="请输入付款单号"
      ></el-input>

      <el-select
        v-model="search.is_pay"
        @change="isPay"
        placeholder="付款单状态"
      >
        <el-option
          v-for="item in is_normalList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.payment_account" placeholder="请选择付款账户">
        <el-option
          v-for="item in accountList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend"
          >供应商</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        class="inp"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend"
          >类别</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>
      <el-select
        v-model="search.status"
        @change="status"
        placeholder="请选择状态"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-input
        style="width: 200px"
        v-model="search.bank"
        placeholder="请输入开户行"
      ></el-input>
      <el-input
        style="width: 200px"
        v-model="search.bank_acount"
        placeholder="请输入银行卡号"
      ></el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <div style="display: inline-block">
        付款时间：
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="dates"
          type="daterange"
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
        <el-select v-model="search.must_pay" placeholder="必付状态">
          <el-option
            v-for="item in must_payList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-checkbox v-model="search.is_hide"
          >排除冻结账款与停用数据</el-checkbox
        >
      </div>
      <div style="display: inline-block">
        挑单时间：
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="tdDate"
          type="daterange"
          :clearable="false"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derives()"
        >导出XlSX</el-button
      >
      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-edit"
        @click="establish()"
        >创建付款单</el-button
      >

      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-error"
        @click="noSettle()"
        >不结算</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      ref="multipleTable"
      tooltip-effect="dark"
      border
      style="width: 100%"
      empty-text="暂无数据"
      class="hidden-xs-only"
      show-summary
      row-key="id"
      :summary-method="getSummaries"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        :selectable="selectable"
        :reserve-selection="true"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="xuhao"
        label="序号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="结算单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="应结金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="pay_amount"
        label="已结金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unliquidated"
        label="未结金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusDesc"
        label="结算状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeMsg"
        label="类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_name"
        label="分类"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="one_class_name"
        label="一级分类"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="codes"
        label="入库单/返货单"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="orgarea_name"
        label="区域"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer"
        label="采购员"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer_code"
        label="采购员code"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="bank"
        label="开户行"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="bank_acount"
        label="银行卡号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="pay_code"
        label="付款单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="gys_time"
        label="供应商审核时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="payment_days"
        label="账期时间(天)"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="payment_date"
        label="到账日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="last_pay_time"
        label="付款时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="contacts"
        label="名字"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tel"
        label="手机号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_num"
        label="商品数量"
        align="center"
      ></el-table-column>
      <el-table-column prop="qty" label="箱数" align="center"></el-table-column>

      <el-table-column
        prop="goods_price"
        label="商品价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="normal_desc"
        label=" 结算状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="payType"
        label=" 付款单状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="created_at"
        label=" 付款单制单时间"
        align="center"
      ></el-table-column>

      <el-table-column label="操作" align="center" width="300px">
        <template slot-scope="scope">
          <el-select
            :disabled="scope.row.cw_status == 2"
            v-model="scope.row.cw_status"
            style="width: 120px"
            size="small"
            @change="statusBtn(scope)"
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>

          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="备注"
      :visible.sync="remark.centerDialogVisible"
      center
      width="500px"
      @close="closes"
    >
      <el-form
        :model="formData"
        ref="formData"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="备注：">
          <el-input
            type="textarea"
            v-model="formData.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="提示"
      center
      :visible.sync="boolPayDialog.centerDialogVisible"
      width="500px"
      @close="noPay"
    >
      <div class="list">
        <div v-for="item in boolPayList" class="listItem" :key="item">
          {{ item }}
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noPay">取 消</el-button>
        <el-button type="primary" @click="topPay">创建付款单</el-button>
      </span>
    </el-dialog>
    <!-- <el-table max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
      class="hidden-sm-and-up"
    >
      <el-table-column prop="buy_order_id" label="订单号" align="center"></el-table-column>
      <el-table-column prop="supplier.name" label="供应商" align="center"></el-table-column>
      <el-table-column prop="amount" label="金额" align="center"></el-table-column>
      <el-table-column prop="statusDesc" label="结算状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="170px">
        <template slot-scope="scope">
          
        </template>
      </el-table-column>
    </el-table>-->
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <div style="height: 0">
      <supplierSearch
        :supplierdialog="supplierdialog"
        @tag="supplierTag"
      ></supplierSearch>
      <commodityDialog
        :commoditydialog="commoditydialog"
        @tag="commodityTag"
      ></commodityDialog>
      <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
      <!-- dialog -->
      <financeDialog :dialog="dialog"></financeDialog>
      <!-- 创建付款单 -->
      <establishDialog
        :dialog="establishDialog"
        @upData="getDatas()"
      ></establishDialog>
    </div>
  </div>
</template>
<script>
import { getAreaList } from "@/api/permission/adminlist";
import { getToken } from "@/utils/auth";
import { getnowDate } from "@/utils/index";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import SupplierSearch from "@/components/grossMargin/supplierSearch";
import ClassDialog from "@/components/grossMargin/classDialog";
import FinanceDialog from "@/components/settlement/financeDialog";
import EstablishDialog from "@/components/settlement/establishDialog";
import Pagination from "@/components/pagination";
import {
  caiwuStatementNewList,
  saveCaiWuStatus,
  PayBillAmount,
  createDaifuPayBill,
  selectBankAccountList
} from "@/api/settlement/settlementList";
export default {
  components: {
    pagination: Pagination,
    financeDialog: FinanceDialog,
    establishDialog: EstablishDialog,
    supplierSearch: SupplierSearch,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      formData: {
        remark: "",
        id: "",
        cw_status: ""
      },
      remark: {
        centerDialogVisible: false
      },
      date: [],
      dates: [],
      tdDate: [],
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        id: ""
      },
      establishDialog: {
        centerDialogVisible: false,
        ids: []
      },
      accountList: [],
      search: {
        status: "",
        searchCode: "",
        pay_code: "",
        start_time: "",
        pay_start_date: "",
        pay_end_date: "",
        end_time: "",
        bank: "",
        bank_acount: "",
        is_pay: "",
        area: "",
        must_pay: "",
        company_id: "",
        is_hide: true,
        payment_account: "",
        td_start_date: "",
        td_end_date: ""
      },
      companyList: [],

      insideArea: {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false
      },
      //dialog信息
      supplierdialog: {
        centerDialogVisible: false
      },
      commoditydialog: {
        centerDialogVisible: false
      },
      classdialog: {
        centerDialogVisible: false
      },
      supplierNameList: {
        name: "",
        code: ""
      },
      classNameList: {
        name: "",
        id: ""
      },
      commodityNameList: {
        name: "",
        code: ""
      },
      handleSelection: [],
      options: [
        {
          value: "-1",
          label: "全部"
        },
        {
          value: 1,
          label: "待结算"
        },
        {
          value: 2,
          label: "已结算"
        },
        {
          value: 3,
          label: "部分结算"
        },
        {
          value: 4,
          label: "冻结"
        },
        {
          value: 5,
          label: "不结算"
        }
      ],
      option: [
        {
          value: 0,
          label: "可结算"
        },
        {
          value: 1,
          label: "冻结"
        },
        {
          value: 2,
          label: "不结算"
        }
      ],
      is_normalList: [
        {
          value: "-1",
          label: "全部"
        },
        {
          value: " 1",
          label: " 未生成付款单"
        },
        {
          value: "2",
          label: "已生成付款单"
        }
      ],
      price: 0,
      lists: [],
      selectedData: [],
      center: {},
      boolPayDialog: {
        centerDialogVisible: false
      },
      boolPayList: [],
      noSettleMsg: false,
      must_payList: [
        {
          value: -1,
          label: "全部"
        },
        {
          value: 0,
          label: "非必付供应商"
        },
        {
          value: 1,
          label: "必付供应商"
        }
      ]
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    this.getData();
    this.getAreaData();
    this.getBankAccountList();
  },
  methods: {
    getBankAccountList() {
      selectBankAccountList().then(res => {
        if (res.code == 0) {
          res.data.data.forEach(data => {
            this.accountList.push({
              value: data.account,
              label: `${data.bank_name}[${data.account}](${data.area_name})`
            });
          });
        }
      });
    },
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        supplier_id: this.supplierNameList.code,
        goods_id: this.commodityNameList.code,
        is_pay: this.search.is_pay == -1 ? "" : this.search.is_pay,
        status: this.search.status == -1 ? "" : this.search.status,
        must_pay: this.search.must_pay == -1 ? "" : this.search.must_pay,
        serial_code: this.search.searchCode,
        pay_code: this.search.pay_code,
        bank: this.search.bank,
        class_id: this.classNameList.id,
        bank_acount: this.search.bank_acount,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
        pay_start_date: this.search.pay_start_date,
        pay_end_date: this.search.pay_end_date,
        orgarea_code_str: this.search.area,
        company_id: this.search.company_id,
        is_hide: this.search.is_hide ? 1 : 0,
        payment_account: this.search.payment_account,
        td_start_date: this.search.td_start_date,
        td_end_date: this.search.td_end_date
      };
      caiwuStatementNewList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              res.payType = res.pay_id > 0 ? "已生成" : "未生成";
              res.statusMsg = res.status == 1 ? "未结算" : "已结算";
              if (res.kind == 10) {
                res.typeMsg = "返货单结算单";
              } else if (res.kind == 11) {
                res.typeMsg = "手动入库单";
              } else if (res.kind == 12 || res.kind == 13) {
                res.typeMsg = "费用结算单";
              } else if (res.kind == 81 || res.kind == 82) {
                res.typeMsg = "改账结算单";
              } else {
                res.typeMsg = "入库单结算单";
              }
              // res.typeMsg = res.type != 10 ? "入库单结算单" : "返货单结算单";
              res.relevance =
                res.type == 0 ? res.storage_bill_id : res.return_id;
              res.codes = res.kind == 10 ? res.return_code : res.storage_code;
              switch (res.cw_status) {
                case 0:
                  res.cw_statusMsg = "可结算";
                  break;
                case 1:
                  res.cw_statusMsg = "冻结";
                  break;
                case 2:
                  res.cw_statusMsg = "不结算";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
            this.lists = res.data.data.filter(res => {
              return res.pay_id > 0;
            });
          }
        })
        .then(() => {
          this.lists.forEach((res, index) => {
            this.$refs.multipleTable.toggleRowSelection(
              this.lists[index],
              false
            );
          });
        })
        .catch(err => {
          console.log(err);
        });
    },

    getDatas() {
      this.$refs.multipleTable.clearSelection();
      this.handleSelection = [];
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        supplier_id: this.supplierNameList.code,
        goods_id: this.commodityNameList.code,
        is_pay: this.search.is_pay == -1 ? "" : this.search.is_pay,
        status: this.search.status == -1 ? "" : this.search.status,
        serial_code: this.search.searchCode,
        pay_code: this.search.pay_code,
        bank: this.search.bank,
        class_id: this.classNameList.id,
        bank_acount: this.search.bank_acount,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
        pay_start_date: this.search.pay_start_date,
        pay_end_date: this.search.pay_end_date,
        orgarea_code_str: this.search.area,
        td_start_date: this.search.td_start_date,
        td_end_date: this.search.td_end_date
      };
      caiwuStatementNewList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              res.payType = res.pay_id > 0 ? "已生成" : "未生成";
              res.statusMsg = res.status == 1 ? "未结算" : "已结算";
              if (res.kind == 10) {
                res.typeMsg = "返货单结算单";
              } else if (res.kind == 11) {
                res.typeMsg = "手动入库单";
              } else if (res.kind == 12 || res.kind == 13) {
                res.typeMsg = "费用结算单";
              } else if (res.kind == 81 || res.kind == 82) {
                res.typeMsg = "改账结算单";
              } else {
                res.typeMsg = "入库单结算单";
              }
              // res.typeMsg = res.type != 10 ? "入库单结算单" : "返货单结算单";
              res.relevance =
                res.type == 0 ? res.storage_bill_id : res.return_id;
              res.codes = res.kind == 10 ? res.return_code : res.storage_code;
              switch (res.cw_status) {
                case 0:
                  res.cw_statusMsg = "可结算";
                  break;
                case 1:
                  res.cw_statusMsg = "冻结";
                  break;
                case 2:
                  res.cw_statusMsg = "不结算";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
            this.lists = res.data.data.filter(res => {
              return res.pay_id > 0;
            });
          }
        })
        .then(() => {
          this.lists.forEach((res, index) => {
            this.$refs.multipleTable.toggleRowSelection(
              this.lists[index],
              false
            );
          });
        })
        .catch(err => {
          console.log(err);
        });
    },
    getAreaData() {
      let data = {
        page: 1,
        limit: 50
      };

      //获取初始化区域列表
      getAreaList(data).then(res => {
        this.insideArea.options = res.data.listdata.map(item => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
      });
    },
    // 搜索区域
    insideRemoteMethods(query) {
      if (query !== "") {
        this.getSupplierLists(query);
      } else {
        this.insideArea.options = [];
      }
    },
    getSupplierLists(query) {
      let data = {
        name: query,
        page: 1,
        limit: 100
      };
      getAreaList(data).then(res => {
        this.insideArea.list = res.data.listdata.map(item => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
        this.insideArea.options = this.insideArea.list.filter(item => {
          this.insideArea.loading = false;
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      });
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true
      };
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true
      };
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        id: ""
      };
      let arrName = [];
      let arrCode = [];
      let arr = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.id);
        arr.push(res.name);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.id = arrCode.join(",");
      this.center["类别"] = arr;
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      let arr = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
        arr.push(res.name);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");

      this.center["供应商"] = arr;
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      let arr = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.id);
        arr.push(res.name);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
      this.center["商品"] = arr;
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          if (index != 2 && index != 3 && index != 4) {
            sums[index] = "";
          } else {
            sums[index] = sums[index].toFixed(4);
          }
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    //禁止选中
    selectable(row, index) {
      if (
        row.pay_id > 0 ||
        row.cw_status != 0 ||
        row.states == "停用" ||
        row.states == "冻结账款"
      ) {
        return false;
      } else {
        return true;
      }
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let status = this.search.status == -1 ? "" : this.search.status;
          let isPay = this.search.is_pay == -1 ? "" : this.search.is_pay;
          let must_pay = this.search.must_pay == -1 ? "" : this.search.must_pay;
          let is_hide = this.search.is_hide ? 1 : 0;
          if (this.date.length > 0) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          if (this.dates.length > 0) {
            this.search.pay_start_date = this.dates[0];
            this.search.pay_end_date = this.dates[1];
          }
          if (this.tdDate.length > 0) {
            this.search.td_start_date = this.tdDate[0];
            this.search.td_end_date = this.tdDate[1];
          }
          let obj = [];
          if (JSON.stringify(this.center) != "{}") {
            if (this.center["供应商"]) {
              if (this.center["供应商"].length > 10) {
                obj.push(
                  "供应商：" +
                    this.center["供应商"].slice(0, 10).join(",") +
                    "..."
                );
              } else if (
                this.center["供应商"].length > 0 &&
                this.center["供应商"].length <= 10
              ) {
                obj.push("供应商：" + this.center["供应商"].join(","));
              }
            }
            if (this.center["商品"]) {
              if (this.center["商品"].length > 10) {
                obj.push(
                  "商品:" + this.center["商品"].slice(0, 10).join(",") + "..."
                );
              } else if (
                this.center["商品"].length <= 10 &&
                this.center["商品"].length > 0
              ) {
                obj.push("商品:" + this.center["商品"].join(","));
              }
            }
            if (this.center["类别"]) {
              if (this.center["类别"].length > 10) {
                obj.push(
                  "类别:" + this.center["类别"].slice(0, 10).join(",") + "..."
                );
              } else if (
                this.center["类别"].length <= 10 &&
                this.center["类别"].length > 0
              ) {
                obj.push("类别:" + this.center["类别"].join(","));
              }
            }
            if (this.center["区域"]) {
              obj.push("区域:" + this.center["区域"]);
            }
            if (this.center["付款单状态"]) {
              obj.push("付款单状态:" + this.center["付款单状态"]);
            }
            if (this.center["状态"]) {
              obj.push("状态:" + this.center["状态"]);
            }
          }
          console.log(obj);
          link.href =
            this.$axios.defaults.baseURL +
            "/api/statement/exportCaiwuStatement?token=" +
            getToken() +
            "&pay_code=" +
            this.search.pay_code +
            "&td_start_date=" +
            this.search.td_start_date +
            "&td_end_date=" +
            this.search.td_end_date +
            "&company_id=" +
            this.search.company_id +
            "&class_id=" +
            this.classNameList.id +
            "&supplier_id=" +
            this.supplierNameList.code +
            "&goods_id=" +
            this.commodityNameList.code +
            "&status=" +
            status +
            "&must_pay=" +
            must_pay +
            "&serial_code=" +
            this.search.searchCode +
            "&bank=" +
            this.search.bank +
            "&bank_acount=" +
            this.search.bank_acount +
            "&is_pay=" +
            isPay +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&pay_start_date=" +
            this.search.pay_start_date +
            "&pay_end_date=" +
            this.search.pay_end_date +
            "&orgarea_code_str=" +
            this.search.area +
            "&is_hide=" +
            is_hide +
            "&searchCenter=" +
            JSON.stringify(obj);
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //导出XLSX
    derives() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          let status = this.search.status == -1 ? "" : this.search.status;
          let isPay = this.search.is_pay == -1 ? "" : this.search.is_pay;
          let must_pay = this.search.must_pay == -1 ? "" : this.search.must_pay;
          let is_hide = this.search.is_hide ? 1 : 0;
          if (this.date.length > 0) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          if (this.dates.length > 0) {
            this.search.pay_start_date = this.dates[0];
            this.search.pay_end_date = this.dates[1];
          }
          if (this.tdDate.length > 0) {
            this.search.td_start_date = this.tdDate[0];
            this.search.td_end_date = this.tdDate[1];
          }
          let obj = [];
          if (JSON.stringify(this.center) != "{}") {
            if (this.center["供应商"]) {
              if (this.center["供应商"].length > 10) {
                obj.push(
                  "供应商：" +
                    this.center["供应商"].slice(0, 10).join(",") +
                    "..."
                );
              } else if (
                this.center["供应商"].length > 0 &&
                this.center["供应商"].length <= 10
              ) {
                obj.push("供应商：" + this.center["供应商"].join(","));
              }
            }
            if (this.center["商品"]) {
              if (this.center["商品"].length > 10) {
                obj.push(
                  "商品:" + this.center["商品"].slice(0, 10).join(",") + "..."
                );
              } else if (
                this.center["商品"].length <= 10 &&
                this.center["商品"].length > 0
              ) {
                obj.push("商品:" + this.center["商品"].join(","));
              }
            }
            if (this.center["类别"]) {
              if (this.center["类别"].length > 10) {
                obj.push(
                  "类别:" + this.center["类别"].slice(0, 10).join(",") + "..."
                );
              } else if (
                this.center["类别"].length <= 10 &&
                this.center["类别"].length > 0
              ) {
                obj.push("类别:" + this.center["类别"].join(","));
              }
            }
            if (this.center["区域"]) {
              obj.push("区域:" + this.center["区域"]);
            }
            if (this.center["付款单状态"]) {
              obj.push("付款单状态:" + this.center["付款单状态"]);
            }
            if (this.center["状态"]) {
              obj.push("状态:" + this.center["状态"]);
            }
          }
          console.log(obj);
          link.href =
            this.$axios.defaults.baseURL +
            "/api/statement/exportCaiwuStatementXlsx?token=" +
            getToken() +
            "&pay_code=" +
            this.search.pay_code +
            "&company_id=" +
            this.search.company_id +
            "&td_start_date=" +
            this.search.td_start_date +
            "&td_end_date=" +
            this.search.td_end_date +
            "&class_id=" +
            this.classNameList.id +
            "&supplier_id=" +
            this.supplierNameList.code +
            "&goods_id=" +
            this.commodityNameList.code +
            "&status=" +
            status +
            "&must_pay=" +
            must_pay +
            "&serial_code=" +
            this.search.searchCode +
            "&bank=" +
            this.search.bank +
            "&bank_acount=" +
            this.search.bank_acount +
            "&is_pay=" +
            isPay +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&pay_start_date=" +
            this.search.pay_start_date +
            "&pay_end_date=" +
            this.search.pay_end_date +
            "&orgarea_code_str=" +
            this.search.area +
            "&is_hide=" +
            is_hide +
            "&searchCenter=" +
            JSON.stringify(obj);
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    confirm() {
      let data = {
        id: this.formData.id,
        cw_status: this.formData.cw_status,
        remark: this.formData.remark
      };
      saveCaiWuStatus(data)
        .then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg || res.message
            });
            this.remark.centerDialogVisible = false;
            if (this.noSettleMsg) {
              this.getDatas();
            } else {
              this.getData();
            }
          } else {
            this.$message({
              type: "error",
              message: res.msg || res.message
            });
            if (this.noSettleMsg) {
              this.getDatas();
            } else {
              this.getData();
            }
          }
        })
        .catch(err => {
          if (this.noSettleMsg) {
            this.getDatas();
          } else {
            this.getData();
          }
        });
    },
    //关闭备注弹窗
    closes() {
      this.formData.remark = "";
      if (this.noSettleMsg) {
        this.getDatas();
      } else {
        this.getData();
      }
    },
    //状态操作
    statusBtn(scope) {
      this.noSettleMsg = false;
      this.formData.id = scope.row.id;
      this.formData.cw_status = scope.row.cw_status;
      this.remark.centerDialogVisible = true;
      this.$refs.multipleTable.clearSelection();
    },
    //不结算
    noSettle() {
      if (this.handleSelection.length == 0) {
        this.$message({
          type: "info",
          message: "请勾选结算单"
        });
      } else {
        this.noSettleMsg = true;
        this.formData.id = this.handleSelection.join(",");
        this.formData.cw_status = 2;
        this.remark.centerDialogVisible = true;
      }
    },
    //创建付款单
    establish() {
      if (this.handleSelection.length == 0) {
        this.$message({
          type: "info",
          message: "请勾选结算单"
        });
      } else {
        let arrId = [];
        this.selectedData.forEach(res => {
          arrId.push(res.id);
        });
        this.boolPay(arrId);
      }
    },
    boolPay(arrId) {
      let arrStr = arrId.join(",");
      let data = {
        idstr: arrStr
      };
      PayBillAmount(data)
        .then(res => {
          console.log(res, 9999);
          if (res.data.type == 1) {
            this.establishDialog = {
              centerDialogVisible: true,
              ids: arrId
            };
          } else {
            this.boolPayDialog.centerDialogVisible = true;
            this.boolPayList = res.data.data;
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: res.msg
          });
        });
    },

    boolPayBill(arrId) {
      let arrStr = arrId.join(",");
      let data = {
        idstr: arrStr
      };
      createDaifuPayBill(data)
        .then(res => {
          if (res.code == 0) {
            this.getData();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: res.msg
          });
        });
    },
    topPay() {
      let arrId = [];
      this.selectedData.forEach(res => {
        arrId.push(res.id);
      });
      this.establishDialog = {
        centerDialogVisible: true,
        ids: arrId
      };
      this.noPay();
    },
    noPay() {
      this.boolPayDialog.centerDialogVisible = false;
      this.boolPayList = [];
    },
    //详情
    particulars(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.id;
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },

    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    // 搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      if (this.dates.length > 0) {
        this.search.pay_start_date = this.dates[0];
        this.search.pay_end_date = this.dates[1];
      }
      if (this.tdDate.length > 0) {
        this.search.td_start_date = this.tdDate[0];
        this.search.td_end_date = this.tdDate[1];
      }
      this.selectedData = [];
      this.pageData.page = 1;
      this.pageData.total = null;
      this.$refs.multipleTable.clearSelection();
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
      this.dates = [];
      this.tdDate=[]
      this.center = {};
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        status: "",
        searchCode: "",
        pay_code: "",
        start_time: "",
        pay_start_date: "",
        pay_end_date: "",
        end_time: "",
        bank: "",
        bank_acount: "",
        is_pay: "",
        area: "",
        must_pay: "",
        company_id: "",
        is_hide: true,
        payment_account: "",
        td_start_date: "",
        td_end_date: ""
      };
      this.insideArea = {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false
      };
      this.getAreaData();
      this.supplierNameList = {
        name: "",
        code: ""
      };
      this.commodityNameList = {
        name: "",
        code: ""
      };
      this.classNameList = {
        name: "",
        id: ""
      };
    },
    //选取
    handleSelectionChange(val) {
      this.handleSelection = [];
      this.selectedData = val;
      val.forEach(res => {
        this.handleSelection.push(res.id);
      });
      this.price = 0;
      this.selectedData.forEach(res => {
        this.price += parseFloat(res.amount);
      });
      this.price = this.price.toFixed(4);
    },
    area() {
      let arr = this.insideArea.options.filter(res => {
        return res.value == this.search.area;
      });
      this.center["区域"] = arr[0].label;
      console.log(this.center);
    },
    isPay() {
      let arr = this.is_normalList.filter(res => {
        return res.value == this.search.is_pay;
      });
      this.center["付款单状态"] = arr[0].label;
    },
    status() {
      let arr = this.options.filter(res => {
        return res.value == this.search.status;
      });
      this.center["状态"] = arr[0].label;
    }
  },
  computed: {
    checkedWeight() {
      let totalWeight = 0;
      this.selectedData.forEach(item => {
        totalWeight += item.weight;
      });
      return totalWeight.toFixed(2);
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.price {
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  height: 40px;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100px;
  margin-left: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
